// pages/storeManage/storeConstruction/storeConstruction.js
import http from '../../../utils/http'
import Dialog from '@vant/weapp/dialog/dialog'
import upload from '../../../utils/upload'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    businessId: '',
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      businessId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  getList() {
    http.post('/api/store/v1/store/images/list', {
      id: this.data.businessId
    }).then(res => {
      this.setData({
        list: res.data.imgList
      })
    })
  },

  updateImage() {
    http.post('/api/store/v1/store/images/update', {
      businessId: this.data.businessId,
      imgList: this.data.list
    }).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '修改成功',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },

  deleteImg(e) {
    Dialog.confirm({
      title: '提示',
      message: '您确定要删除这个图片嘛',
    }).then(() => {
      const list = this.data.list
      list.splice(e.currentTarget.dataset.index, 1)
      this.setData({
        list
      })
      this.updateImage()
    })
  },

  // 头像
  uploadAvatar(){
    let that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        upload.uploadMed(tempFilePaths[0]).then(img => {
          that.setData({
            list: that.data.list.concat(img)
          })
          that.updateImage()
        })
      }
    })
  },

  previewImg(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,//点击显示的图片
      urls: this.data.list//全部图片
    })
  }
})